//=======================================================================
//功能: 			JavaScript 分页工具类  [自定义数据显示的样式]
//author:		岳静
//e-mail: 		yuejing0129@126.com
//QQ:			503490146
//date: 		2012-08-01
//version: 		1.1
//update info	增加了样式的切换功能
//=======================================================================

/**
 * 获取Page对象
 * _page		  : 创建的Page对象的名称
 * _callback	  : 查询的回调函数
 * _contentId     : 显示数据的内容ID
 * _pageFootId    : 显示page的操作ID
 */
var Page = function(_page, _callback, _contentId, _pageFootId) {
	this.pageName = _page;
	this.callback = _callback;
	this.contentId = _contentId;
	this.pageFootId = _pageFootId;
};

//样式集合[值于样式文件的名称一致]
Page.prototype.pageStyle = { defaultStyle : "default", rightOneStyle : "right_one"
	, conciseOneStyle : "concise_one", conciseTwoStyle : "concise_two" };
//上一页 下一页等的显示
Page.prototype.pageShowContent = { up : "上一条", down : "下一条" };
//当前页[默认为第一页]
Page.prototype.page = 1;
//总页数大小
Page.prototype.countPage;
//每页大小[默认为10条]
Page.prototype.size = 10;
//page间距大小[默认为2,显示格式为..45 6 78..]
Page.prototype.pageDiffNum = 2;
//批量操作的信息的字符串
Page.prototype.batchOperate;
//开始的字符串
Page.prototype.beginString;
//结束字符串
Page.prototype.endString;
//每页大小切换的值[默认为: 10 20 30]
Page.prototype.pageSizeArr = [10, 20, 30];

/**
 * 给控件赋值
 * @param _name : 控件名称
 * @param _val  : 赋值内容
 */
Page.prototype.setWidget = function(_name, _val) {
	if(_name != undefined && _name != "") {
		document.getElementById(_name).innerHTML = _val;
	}
};

/**
 * 添加请求中方法
 * @param _message : 传入的加载消息
 */
Page.prototype.pageBeforeSend = function(_message) {
	if(_message === undefined) {
		_message = "加载信息中...";
	}
	if(this.beginString != undefined) {
		_message = this.beginString + _message;
	}
	if(this.endString != undefined) {
		_message += this.endString;
	}
	this.setWidget(this.contentId, _message);
};

/**
 * 添加失败处理结果方法
 * @param _message : 传入的异常消息
 */
Page.prototype.pageError = function(_message) {
	if(_message === undefined) {
		_message = "加载信息异常, 请稍候再试...";
	}
	if(this.beginString != undefined) {
		_message = this.beginString + _message;
	}
	if(this.endString != undefined) {
		_message += this.endString;
	}
	this.setWidget(this.contentId, _message);
};

/**
 * 生成分页标签的信息[上一页 下一页 尾页 等信息...]
 * @param _json
 * @param _data
 * 			resultFn      : 存在数据处理的函数
 * 			dataNull      : 不存在数据的提示
 * 			pageFootStyle : pageFoot的样式[default: 默认的]
 * @returns {String}
 */
Page.prototype.pageOperate = function(_json, _data) {

	var result = "";
	if(this.beginString != undefined) {
		result = this.beginString;
	}
	//处理数据
	var _bool = false;
	if(_json != undefined) {
		if(_json.DATA != undefined && _json.DATA.length > 0) {
			$.each(_json.DATA, function(i, obj) {
				result += _data.resultFn(obj, i);
			});
		} else {
			result += (_data.dataNull === undefined ? "没有查找到数据!" : _data.dataNull);
			_bool = true;
		}
	} else {
		result += (_data.dataNull === undefined ? "没有查找到数据!" : _data.dataNull);
		_bool = true;
	}

	if(this.endString != undefined) {
		result += this.endString;
	}
	this.setWidget(this.contentId, result);
	if(_bool) {
		//设置pageFoot信息
		this.setWidget(this.pageFootId, "");
		return;
	}

	//设置总页数信息
	this.countPage = _json.countPAGE;

	//根据样式生成pageFoot
	if(_data.pageFootStyle === undefined) {
		_data.pageFootStyle = this.pageStyle.defaultStyle;
	}
	switch (_data.pageFootStyle) {
	case this.pageStyle.defaultStyle:
		//默认样式
		result = this.defaultStyleFn();
		break;
	case this.pageStyle.rightOneStyle:
		//向右靠样式1
		result = this.rightOneStyleFn();
		break;
	case this.pageStyle.conciseOneStyle:
		//简洁样式1[上一页 下一页]
		result = this.conciseOneStyleFn();
		break;
	case this.pageStyle.conciseTwoStyle:
		//简洁样式2[上一页 下一页]
		result = this.conciseTwoStyleFn();
		break;
	default:
		//默认样式
		result = this.defaultStyleFn();
	break;
	}
	//设置pageFoot信息
	this.setWidget(this.pageFootId, result);
};

/**
 * 分页的信息[上一页 下一页 尾页 等信息...]
 * @param _type
 * @param num
 */
Page.prototype.pageFun = function(_type, num) {
	switch (_type) {
	case 0:
		//首页
		this.page = 1;
		break;
	case 1:
		//上一页
		if(this.page > 1)
			this.page --;
		break;
	case 2:
		//下一页
		if(this.page < this.countPage)
			this.page ++;
		break;
	case 3:
		//末页
		this.page = this.countPage;
		break;
	case 4:
		//自定义页码
		this.page = num;
		break;
	case 5:
		//自定义每页的大小
		this.size = num;
		this.page = 1;
		break;
	}
	this.callback();
};

/**
 * 默认的样式
 * @param _countPage : 总页数[单独调用需要传入]
 */
Page.prototype.defaultStyleFn = function(_countPage) {
	//处理总页数
	if(_countPage != undefined) {
		this.countPage = _countPage;
	}
	//设置pageFoot容器的样式
	var result = "<div class='" + this.pageStyle.defaultStyle + "'>";
	//批量操作的字符串
	if(this.batchOperate != undefined) {
		result += this.batchOperate + "&nbsp;";
	}

	result += "每页" + this.size + "条&nbsp;&nbsp;";
	result += "第&nbsp;" + this.page + "页&nbsp;&nbsp;";
	result += "共&nbsp;" + this.countPage + "页&nbsp;&nbsp;";

	var _page = this.page;

	if(this.page > 1)
		result += "<a href='javascript: " + this.pageName + ".pageFun(1)'>上一页</a>&nbsp;";

	var startNum;
	//当前页减去距离页大于1
	if( (_page - this.pageDiffNum) > 1 ) {
		result += "<a href='javascript: " + this.pageName + ".pageFun(0)'>1</a>&nbsp;..&nbsp;";
		startNum = (_page - this.pageDiffNum);
	} else {
		if( (_page + this.pageDiffNum) > this.countPage )
			_page = _page + this.pageDiffNum;
		startNum = 1;
	}
	//当前页加上距离页小于总页数
	var endResult = undefined;
	if( (_page + this.pageDiffNum) < this.countPage ) {
		endResult = "..&nbsp;<a href='javascript: " + this.pageName + ".pageFun(3)'>" + this.countPage + "</a>&nbsp;";
		if( (_page + this.pageDiffNum) < this.countPage )
			endNum = (_page + this.pageDiffNum);
		else
			endNum = this.countPage;
	} else {
		endNum = this.countPage;
		if( (_page + this.pageDiffNum) < this.countPage )
			startNum = endNum - (2 * this.pageDiffNum + 1);
	}
	//处理中间的数字
	for(var i = startNum; i <= endNum; i ++) {
		if(i != this.page) {
			result += "<a href='javascript: " + this.pageName + ".pageFun(4, " + i + ")'>" + i + "</a>&nbsp;";
		} else {
			result += "<span class='current'>" + i + "</span>&nbsp;";
		}
	}
	if(endResult != undefined)
		result += endResult;
	if(this.page < this.countPage)
		result += "<a href='javascript: " + this.pageName + ".pageFun(2)'>下一页</a>&nbsp;";

	//处理size大小
	result += "&nbsp;页大小&nbsp;";
	for(var i = 0; i < this.pageSizeArr.length; i ++) {
		if(this.pageSizeArr[i] != this.size) {
			result += "<a href='javascript: " + this.pageName + ".pageFun(5, " + this.pageSizeArr[i] + ")'>" + this.pageSizeArr[i] + "</a>&nbsp;";
		} else {
			result += "<span class='current'>" + this.pageSizeArr[i] + "</span>&nbsp;";
		}
	}
	result += "</div>";
	return result;
};


/**
 * 向右靠的样式1
 * @param _countPage : 总页数[单独调用需要传入]
 */
Page.prototype.rightOneStyleFn = function(_countPage) {
	//处理总页数
	if(_countPage != undefined) {
		this.countPage = _countPage;
	}
	//设置pageFoot容器的样式
	var result = "<div class='" + this.pageStyle.rightOneStyle + "'>";
	//批量操作的字符串
	if(this.batchOperate != undefined) {
		result += this.batchOperate + "&nbsp;";
	}
	var _page = this.page;

	if(this.page > 1)
		result += "<span><a href='javascript: " + this.pageName + ".pageFun(1)'><</a></span>";

	var startNum;
	//当前页减去距离页大于1
	if( (_page - this.pageDiffNum) > 1 ) {
		result += "<span><a href='javascript: " + this.pageName + ".pageFun(0)'>1</a></span>&nbsp;..&nbsp;";
		startNum = (_page - this.pageDiffNum);
	} else {
		if( (_page + this.pageDiffNum) > this.countPage )
			_page = _page + this.pageDiffNum;
		startNum = 1;
	}
	//当前页加上距离页小于总页数
	var endResult = undefined;
	if( (_page + this.pageDiffNum) < this.countPage ) {
		endResult = "..&nbsp;<a href='javascript: " + this.pageName + ".pageFun(3)'>" + this.countPage + "</a>&nbsp;";
		if( (_page + this.pageDiffNum) < this.countPage )
			endNum = (_page + this.pageDiffNum);
		else
			endNum = this.countPage;
	} else {
		endNum = this.countPage;
		if( (_page + this.pageDiffNum) < this.countPage )
			startNum = endNum - (2 * this.pageDiffNum + 1);
	}
	//处理中间的数字
	for(var i = startNum; i <= endNum; i ++) {
		if(i != this.page) {
			result += "<a href='javascript: " + this.pageName + ".pageFun(4, " + i + ")'>" + i + "</a>";
		} else {
			result += "<span class='current'>" + i + "</span>";
		}
	}
	if(endResult != undefined)
		result += endResult;
	if(this.page < this.countPage)
		result += "<a href='javascript: " + this.pageName + ".pageFun(2)'>></a>";
	result += "</div>";
	return result;
};

/**
 * 简洁样式1[上一页 下一页]
 * @param _countPage : 总页数[单独调用需要传入]
 */
Page.prototype.conciseOneStyleFn = function(_countPage) {
	//处理总页数
	if(_countPage != undefined) {
		this.countPage = _countPage;
	}
	//设置pageFoot容器的样式
	var result = "<div class='" + this.pageStyle.conciseOneStyle + "'>";
	//批量操作的字符串
	if(this.batchOperate != undefined) {
		result += this.batchOperate + "&nbsp;";
	}

	if(this.page > 1)
		result += "<a href='javascript: " + this.pageName + ".pageFun(1)'>" + this.pageShowContent.up + "</a>";

	result += "&nbsp;<span class='current'>" + this.page + "</span>&nbsp;";
	if(this.page < this.countPage)
		result += "<a href='javascript: " + this.pageName + ".pageFun(2)'>" + this.pageShowContent.down + "</a>";
	result += "</div>";
	return result;
};

/**
 * 简洁样式2[上一页 下一页]
 * @param _countPage : 总页数[单独调用需要传入]
 */
Page.prototype.conciseTwoStyleFn = function(_countPage) {
	//处理总页数
	if(_countPage != undefined) {
		this.countPage = _countPage;
	}
	//设置pageFoot容器的样式
	var result = "<div class='" + this.pageStyle.conciseTwoStyle + "'>";
	//批量操作的字符串
	if(this.batchOperate != undefined) {
		result += this.batchOperate + "&nbsp;";
	}

	if(this.page > 1)
		result += "<a href='javascript: " + this.pageName + ".pageFun(1)'>" + this.pageShowContent.up + "</a>";

	result += "&nbsp;<span class='current'>" + this.page + "</span>&nbsp;";
	if(this.page < this.countPage)
		result += "<a href='javascript: " + this.pageName + ".pageFun(2)'>" + this.pageShowContent.down + "</a>";
	result += "</div>";
	return result;
};